package com.cjlgb.design.gateway.filter.ratelimit;

import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.ratelimit.KeyResolver;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.net.InetSocketAddress;

/**
 * @author Bin
 * @date 2019-05-14 17:07
 * description:限流解析器
 */
@Component
@Slf4j
public class HostAddrKeyResolver  implements KeyResolver {

    @Override
    public Mono<String> resolve(ServerWebExchange exchange) {
        InetSocketAddress socketAddress = exchange.getRequest().getRemoteAddress();
        if (null == socketAddress){
            return Mono.empty();
        }
        return Mono.just(socketAddress.getAddress().getHostAddress());
    }

}
